package com.jsoft.manager;

import com.jsoft.model.UserDao;
import com.jsoft.model.core.ManagerImpl;

import java.util.List;

/**
 * Date: 15.08.13
 * Time: 23:22
 */

public class UserManagerImpl extends ManagerImpl<UserDao> implements UserManager {
    public UserManagerImpl() {
        super(UserDao.class);
    }

    @Override
    public UserDao getUserByLoginAndPassword(String login, String password) {
        List<UserDao> userDaoList = getEntityManager().createQuery("select u from UserDao u where u.login = '"
                + login + "' and u.password = '" + password + "'" + " and u.isDeleted is not true",
                UserDao.class).getResultList();
        if (userDaoList.size() != 0) {
            return userDaoList.get(0);
        }
        return null;
    }

    @Override
    public UserDao getUserByPassword(String password) {
        List<UserDao> userDaoList = getEntityManager().createQuery("select u from UserDao u where u.password = '"
                + password + "'" + " and u.isDeleted is not true",
                UserDao.class).getResultList();
        if (userDaoList.size() != 0) {
            return userDaoList.get(0);
        }
        return null;
    }

    @Override
    public UserDao getUserByCardNumber(String cardNumber) {
        List<UserDao> userDaoList = getEntityManager().createQuery("select u from UserDao u where u.cardNumber = '"
                + cardNumber + "'" + " and u.isDeleted is not true",
                UserDao.class).getResultList();
        if (userDaoList.size() != 0) {
            return userDaoList.get(0);
        }
        return null;
    }
}
